home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.13 / terrain.h < prev   
Encoding:
C/C++ Source or Header  |  2006-06-29  |  2.0 KB  |  101 lines

  1. #ifndef _TERRAIN_
  2. #define _TERRAIN_
  3.  
  4. #include <d3dx9.h>
  5. #include <vector>
  6. #include "heightmap.h"
  7. #include "debug.h"
  8. #include "shader.h"
  9. #include "object.h"
  10.  
  11. struct TERRAINVertex
  12. {
  13.     TERRAINVertex(){}
  14.     TERRAINVertex(D3DXVECTOR3 pos, D3DXVECTOR2 _uv1, D3DXVECTOR2 _uv2)
  15.     {
  16.         position = pos;
  17.         normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
  18.         uv1 = _uv1;
  19.         uv2 = _uv2;
  20.     }
  21.  
  22.     D3DXVECTOR3 position, normal;
  23.     D3DXVECTOR2 uv1, uv2;
  24.  
  25.     static const DWORD FVF;
  26. };
  27.  
  28. struct PATCH{
  29.     PATCH();
  30.     ~PATCH();
  31.     void Release();
  32.     HRESULT CreateMesh(HEIGHTMAP &hm, RECT source, IDirect3DDevice9* Dev);
  33.     void Render();
  34.  
  35.     IDirect3DDevice9* m_pDevice;
  36.     ID3DXMesh *m_pMesh;
  37. };
  38.  
  39. struct MAPTILE{
  40.     MAPTILE()    //Set everything to 0
  41.     {
  42.         m_type = m_set = 0; 
  43.         m_height = m_cost = 0.0f;
  44.         m_walkable = false;
  45.         m_pParent = NULL;
  46.  
  47.         for(int i=0;i<8;i++)
  48.             m_pNeighbors[i] = NULL;
  49.     }
  50.  
  51.     int m_type, m_set;
  52.     float m_height, m_cost;
  53.     bool m_walkable;
  54.     MAPTILE* m_pNeighbors[8];
  55.  
  56.     // Pathfinding variables
  57.     INTPOINT m_mappos;
  58.     float f,g;
  59.     bool open, closed;
  60.     MAPTILE *m_pParent;
  61. };
  62.  
  63. class TERRAIN{
  64.     friend class APPLICATION;
  65.     public:
  66.         TERRAIN();        
  67.         void Init(IDirect3DDevice9* Dev, INTPOINT _size);
  68.         void Release();
  69.         void GenerateRandomTerrain(int numPatches);
  70.         void CreatePatches(int numPatches);
  71.         void CalculateAlphaMaps();
  72.         void AddObject(int type, INTPOINT mappos);
  73.         void Render();
  74.  
  75.         //Pathfinding
  76.         bool Within(INTPOINT p);    //Test if a point is within the bounds of the terrain
  77.         void InitPathfinding();
  78.         void CreateTileSets();
  79.         std::vector<INTPOINT> GetPath(INTPOINT start, INTPOINT goal);
  80.         MAPTILE* GetTile(int x, int y);
  81.         MAPTILE* GetTile(INTPOINT p){return GetTile(p.x, p.y);}
  82.  
  83.         //Public variables
  84.         MAPTILE *m_pMapTiles;
  85.  
  86.     private:
  87.  
  88.         INTPOINT m_size;
  89.         IDirect3DDevice9* m_pDevice; 
  90.  
  91.         HEIGHTMAP *m_pHeightMap;
  92.         std::vector<PATCH*> m_patches;
  93.         std::vector<IDirect3DTexture9*> m_diffuseMaps;
  94.         std::vector<OBJECT> m_objects;
  95.         IDirect3DTexture9* m_pAlphaMap;
  96.         SHADER m_terrainPS;
  97.  
  98.         D3DMATERIAL9 m_mtrl;
  99. };
  100.  
  101. #endif